package com.mediacloud.app.newsmodule.view.share

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.mediacloud.app.assembly.util.toastCenter
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig
import com.sina.weibo.sdk.api.ImageObject
import com.sina.weibo.sdk.api.TextObject
import com.sina.weibo.sdk.api.WeiboMultiMessage
import com.sina.weibo.sdk.auth.AuthInfo
import com.sina.weibo.sdk.common.UiError
import com.sina.weibo.sdk.openapi.IWBAPI
import com.sina.weibo.sdk.openapi.WBAPIFactory
import com.sina.weibo.sdk.share.WbShareCallback
import com.tenma.ventures.share.login.LoginConstant
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.util.*

class WbShareActivity : AppCompatActivity(),
    WbShareCallback {

    private var mWBAPI: IWBAPI? = null
    private var articleItem: ArticleItem? = null
    private var type: Int? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        type = intent.getIntExtra("type", 2)

        val authInfo = AuthInfo(
            this,
            LoginConstant.WB_APP_ID, LoginConstant.WB_APP_URL, LoginConstant.WB_SCOPE
        )
        mWBAPI = WBAPIFactory.createWBAPI(this)
        mWBAPI?.registerApp(this, authInfo)
        mWBAPI?.setLoggerEnable(true)
        if (type == 1) {
            articleItem = intent.getParcelableExtra("data")
            goWbLinkShare()
        } else {
            val stringExtra = intent.getStringExtra("path")
            val param = stringExtra?.let { File(it) }
            var bitmap = BitmapFactory.decodeFile(param?.path)
            if (bitmap != null) {
                doWeiboShare(bitmap)
            } else {
                toastCenter(this, "获取图片信息出错")
            }
        }
    }


    fun doWeiboShare(bitmap: Bitmap) {
        val message = WeiboMultiMessage()
        // 分享图片
        val imageObject = ImageObject()
        imageObject.setImageData(bitmap)
        message.imageObject = imageObject

        mWBAPI!!.shareMessage(this, message, true)
    }

    private fun goWbLinkShare() {
        val weiboMessage = WeiboMultiMessage()
//        val mediaObject = WebpageObject()
//
//        mediaObject.identify = UUID.randomUUID().toString()
//        mediaObject.title = articleItem?.title
//        if (!TextUtils.isEmpty(articleItem!!.movie.shareSummary)) {
//            mediaObject.description = articleItem!!.movie.shareSummary
//        }
//        mediaObject.actionUrl = articleItem?.url
//        mediaObject.defaultText = "分享链接"


        val textObject = TextObject()
        textObject.text = articleItem?.title + articleItem?.url
        weiboMessage.textObject = textObject

        val imageObject = ImageObject()
        Glide.with(this)
            .asBitmap()
            .load(getShareLogo(articleItem))
            .into(object : SimpleTarget<Bitmap>() {
                override fun onResourceReady(bitmap: Bitmap, transition: Transition<in Bitmap>?) {
                    imageObject.setImageData(bitmap)
                    weiboMessage.imageObject = imageObject
                    mWBAPI?.shareMessage(this@WbShareActivity, weiboMessage, false)
                }

                override fun onLoadFailed(errorDrawable: Drawable?) {
                    super.onLoadFailed(errorDrawable)
                    mWBAPI?.shareMessage(this@WbShareActivity, weiboMessage, false)
                }
            })

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (mWBAPI != null) {
            mWBAPI!!.doResultIntent(data, this)
        }
    }

    override fun onComplete() {
        toastCenter(this, "分享成功")
        finish()
    }

    override fun onError(error: UiError) {
        toastCenter(this, "分享失败:" + error.errorMessage)
        finish()
    }

    override fun onCancel() {
        toastCenter(this, "分享取消")
        finish()
    }


    protected fun getShareLogo(articleItem: ArticleItem?): String {
        //20200227新增需求，从新的字段去取分享的logo
        if (articleItem != null) {
            if (articleItem.movie != null) {
                if (!TextUtils.isEmpty(articleItem.movie.shareThumbnail)) {
                    return articleItem.movie.shareThumbnail
                }
            }
        }
        if (articleItem != null) {
            if (articleItem.customColumns != null) {
                if (!TextUtils.isEmpty(articleItem.customColumns.share_logo)) return articleItem.customColumns.share_logo
            }
        }
        if (articleItem != null) {
            if (!TextUtils.isEmpty(articleItem.logo)) {
                return articleItem.logo
            }
        }
        //return "https://image.junhao.mil.cn/spider/pic/upload/image/2022/06/28/ec28be0655fe443ebe58af94d2b5e6fe.png"
        return AppFactoryGlobalConfig.ShareIconUrlDef;
    }
}